package com.kuangstudy.config.mvc;

import com.kuangstudy.handler.LogInterceptor;
import com.kuangstudy.handler.LoginInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Description:
 * Author: yykk Administrator
 * Version: 1.0
 * Create Date Time: 2021/11/15 21:49.
 * Update Date Time:
 *
 * @see
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    // 1：把LoginInterceptor放入到ioc容器中
    @Bean
    public LoginInterceptor getLoginInterceptor(){
        return new LoginInterceptor();
    }

    @Bean
    public LogInterceptor getLogInterceptor(){
        return new LogInterceptor();
    }



    //:2：注册拦截器的方法
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // registry.addInterceptor(getLoginInterceptor()) 注册拦截器
//        registry.addInterceptor(getLoginInterceptor())
//                // 拦截器进入的路由规则是什么，addPathPatterns 代表以下配置的路由都会进行LoginInterceptor
//                // 求执行一次在进入路由springmvc的具体方法中
//                .addPathPatterns("/detail/**","/index","/blog","/collect","/acv","/api/**","/blog/**")
//                // 代表不拦截路由
//                .excludePathPatterns("/detail/new/**");

//        registry.addInterceptor(getLogInterceptor())
//                // 拦截器进入的路由规则是什么，addPathPatterns 代表以下配置的路由都会进行LoginInterceptor
//                // 求执行一次在进入路由springmvc的具体方法中
//                .addPathPatterns("/detail/**","/","/index")
//                // 代表不拦截路由
//                .excludePathPatterns("/detail/new/**");
    }


    @Value("${file.staticPatternPath}")
    private String staticPatternPath;
    @Value("${file.uploadFolder}")
    private String uploadFolder;//
    /*
     * @Author 徐柯
     * @Description 静态资源路径映射
     *  其实就自定义静态资源服务器
     * @Date 20:11 2021/4/18
     * @Param [registry]
     * @return void
     **/
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler(staticPatternPath).addResourceLocations("file:" + uploadFolder);
    }


}
